name: release-npm

on:
  push:
    branches:
      - latest
    paths:
      - '**/package.json'
      - '.github/workflows/**'

jobs:
  publish:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        package:
          - name: tauri.js
            registryName: tauri
            path: cli/tauri.js
    steps:
      - uses: actions/checkout@v2
        with:
          ref: latest
      - uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: 'https://registry.npmjs.org'
      - name: get version
        working-directory: ${{ matrix.package.path }}
        run: echo ::set-env name=PACKAGE_VERSION::$(node -p "require('./package.json').version")
      - name: check published version
        run: echo ::set-env name=PUBLISHED_VERSION::$(npm view ${{ matrix.package.registryName }} version)
      - name: yarn install
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        working-directory: ${{ matrix.package.path }}
        run: yarn
      - name: npm pack
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        working-directory: ${{ matrix.package.path }}
        run: |
          echo "pack dir:"
          ls
          npm pack
          echo "We will publish:" $PACKAGE_VERSION
          echo "This is current latest:" $PUBLISHED_VERSION
          echo "post pack dir:"
          ls
      - name: yarn audit
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        working-directory: ${{ matrix.package.path }}
        run: |
          echo "# Yarn Audit Results" | tee -a ${{runner.workspace }}/notes.md
          echo "" >> ${{runner.workspace }}/notes.md
          echo "<details>" >> ${{runner.workspace }}/notes.md
          echo "<summary>click to view</summary>" >> ${{runner.workspace }}/notes.md
          echo "" >> ${{runner.workspace }}/notes.md
          echo "\`\`\`" >> ${{runner.workspace }}/notes.md
          yarn audit 2>&1 | tee -a ${{runner.workspace }}/notes.md
          echo "\`\`\`" >> ${{runner.workspace }}/notes.md
          echo "</details>" >> ${{runner.workspace }}/notes.md
          echo "" >> ${{runner.workspace }}/notes.md
      - name: Publish ${{ matrix.package.name }}
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        working-directory: ${{ matrix.package.path }}
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          echo "# NPM Package Publish" | tee -a ${{runner.workspace }}/notes.md
          echo "\`\`\`" >> ${{runner.workspace }}/notes.md
          set -o pipefail && npm publish 2>&1 | tee -a ${{runner.workspace }}/notes.md
          echo "\`\`\`" >> ${{runner.workspace }}/notes.md
      - name: Create Release
        id: create_npm_release
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        uses: jbolda/create-release@v1.1.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ matrix.package.name }}-v${{ env.PACKAGE_VERSION }}
          release_name: "Release ${{ matrix.package.name }} v${{ env.PACKAGE_VERSION }} [npmjs.com]"
          bodyFromFile: ./../notes.md
          draft: false
          prerelease: false
      - name: Upload Release Asset
        id: upload-release-asset
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        uses: actions/upload-release-asset@v1.0.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_npm_release.outputs.upload_url }}
          asset_path: ./${{ matrix.package.path }}/${{ matrix.package.registryName }}-${{ env.PACKAGE_VERSION }}.tgz
          asset_name: ${{ matrix.package.registryName }}-${{ env.PACKAGE_VERSION }}.tgz
          asset_content_type: application/x-gtar
      - name: Tangle Release
        id: tangle_release
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        uses: iotaledger/gh-tangle-release@v0.6.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          IOTA_SEED: ${{ secrets.IOTA_SEED }}
          IOTA_NODE: "https://iotanode.us:14267"
        with:
          tag_name: ${{ matrix.package.name }}-v${{ env.PACKAGE_VERSION }}
          comment: "Release ${{ matrix.package.name }} v${{ env.PACKAGE_VERSION }} [npmjs.com]"
